package Homework.cms.servlet;

import Homework.cms.user.Member;
import Homework.cms.user.MemberService;

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;

public class LogServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");     //告诉它当前文档的编码
        response.setHeader("Content-Type","text/html;utf-8");    //告诉浏览器文档类型

        String phone=request.getParameter("phone");
        String password=request.getParameter("pwd");
        String check=request.getParameter("check"); //获得 “记住我” 选项框里的内容
        String autologin=request.getParameter("autologin"); //获得 “自动登录” 选项框里的内容
        String vali1=request.getParameter("vali");                          //这是用户输入的验证码
        String vali=request.getSession().getAttribute("vali").toString();   //这是图片里验证码的内容

        PrintWriter out=response.getWriter();   //获取一个打印流，向网页输出打印
        MemberService memberService=new MemberService();

        if(!vali1.equals(vali)){
            out.println("验证码错误，请重新登录！");
        }
        else {
            Member member=memberService.login(phone,password);
            if(member!=null){


                out.println("登录成功！3秒后跳转到首页！");
                out.println("<br>");
                out.println(member.toString());
                response.setHeader("Refresh","3;url=http://localhost:8080/webtest/Homework/Qiantai/index.jsp");


                if(check.equals("on")){     //是否选中 “记住我”
                    //把cookie存下来
                    Cookie nameCookie=new Cookie("name",phone);
                    Cookie pwdCookie=new Cookie("pwd",password);
                    nameCookie.setMaxAge(120);
                    pwdCookie.setMaxAge(120);
                    response.addCookie(nameCookie);
                    response.addCookie(pwdCookie);
                }

                if(autologin.equals("on")){
                    Cookie autologinCookie=new Cookie("autologin",autologin);
                    autologinCookie.setMaxAge(120);
                    response.addCookie(autologinCookie);
                }

                request.getSession(true).setAttribute("username",phone);
                System.out.println(phone+"进行登录操作");
                HttpSession session=request.getSession();
////                session.setAttribute("username",phone);
////                ServletContext servletContext = request.getSession().getServletContext();
////                List<String> users = (List)servletContext.getAttribute("users");
////                users.add(phone);
////                servletContext.setAttribute("users", users);
                Object lasturl=session.getAttribute("lasturl");
                String url=null;
                if(lasturl!=null){
                    url=lasturl.toString();
                }
                if(url!=null && url.length()>0){
                    response.sendRedirect(url);
                }
            }
            else {
                out.println(phone+"   "+password);
                out.println("手机号或密码错误，请重新登录！");
            }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
